<?php

// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------

namespace app\adminapi\logic\setting;


use app\common\model\setting\CarType;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * CarType逻辑
 * Class CarTypeLogic
 * @package app\adminapi\logic\setting
 */
class CarTypeLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/27 10:41
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            CarType::create([
                'name' => $params['name'],
                'price' => $params['price'],
                'image' => $params['image'],
                'description' => $params['description'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/27 10:41
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            CarType::where('id', $params['id'])->update([
                'name' => $params['name'],
                'price' => $params['price'],
                'image' => $params['image'],
                'description' => $params['description'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/27 10:41
     */
    public static function delete(array $params): bool
    {
        return CarType::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/27 10:41
     */
    public static function detail($params): array
    {
        return CarType::findOrEmpty($params['id'])->toArray();
    }

    public static function getAllData()
    {
        return CarType::select()->toArray();
    }
}